package com.jinloes.webfit;

import com.jinloes.webfit.persistence.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;

@Service("userService")
public class UserDetailsManagerImpl implements UserDetailsManager {

    @Autowired
    @Qualifier("userDao")
    public UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void createUser(UserDetails userDetails) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void updateUser(UserDetails userDetails) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void deleteUser(String s) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void changePassword(String username, String password) {

        UserDetails userDetails = null;
        User user = userDao.getUserByName(username);

        if (user == null) {
            System.out.println("USERNAME NOT FOUND: " + username);
          throw new UsernameNotFoundException("user not found: " + username);
        }

        user.setPassword(password);
        userDao.updateUser(user);
    }

    public boolean userExists(String s) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException, DataAccessException {

        UserDetails userDetails = null;
        User user = userDao.getUserByName(name);

        if (user == null) {
          throw new UsernameNotFoundException("user not found");
        }

        String username = user.getUsername();
        String password = user.getPassword();
        boolean enabled = user.getAccountStatus().equals("active");
        //(user.getRole().compareTo(User.Role.USER)) >=0;
        boolean accountNonExpired = enabled;
        boolean credentialsNonExpired = enabled;
        boolean accountNonLocked = enabled;

        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        if(user.getRole() == null && enabled) {
            authorities.add(new GrantedAuthorityImpl("USER"));
        }
        else if(user.getRole().compareTo(User.Role.ADMIN) >=0) {
            authorities.add(new GrantedAuthorityImpl("USER"));
            authorities.add(new GrantedAuthorityImpl("ADMIN"));
        }

        userDetails = new org.springframework.security.core.userdetails.User(username, password, enabled,
          accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        return userDetails;
    }
}
